﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using Maticsoft.Common;
using CustomsSystem.Model;

namespace WEB.workPlan
{
    public partial class workPlanNView : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        CustomsSystem.BLL.T_workPlan bll = new CustomsSystem.BLL.T_workPlan();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            string strSql = " 1=1 ";
            DateTime timeStop = DateTime.Now;
            //查询过去和当天时间
            strSql = strSql + " and planDate>=1900-1-1 and planDate<=getDate() ";
            //筛选未完成的
            strSql += " and states = 1 ";
            strSql += " order by planDate desc";
            ds = bll.GetList(strSql);
            if (!IsPostBack)
            {
                gvPlan.DataSource = ds;
                gvPlan.DataBind();
            }
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            //DataSet ds = new DataSet();
            ds = bll.GetList (Query());
            gvPlan.DataSource = ds;
            gvPlan.DataBind();
        }

        
        //查询条件
        public string Query()
        {
            string strQuery = ddlQuery.SelectedValue;
            string strSql = " 1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                switch (ddlQuery.SelectedIndex)  //根据选项传不同的SQL语句
                {
                    case 1:
                    case 0:
                    case 2:
                    case 3:
                        strSql += string.Format(" and {0} like '%{1}%' ", strQuery, tbQuery.Text.Trim());
                        break;
                    default: if (!PageValidate.IsNumber(tbQuery.Text.Trim())) tbQuery.Text = "0";
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                        break;
                }
            }
            DateTime timeStop = DateTime.Now;
            //查询过去和当天时间
            strSql = strSql + " and planDate>=1900-1-1 and planDate<=getDate() ";
            //筛选未完成的
            strSql += " and states = 1 ";
            strSql += " order by planDate desc";
            return strSql;
        }



        protected void gvPlan_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddl = e.Row.Cells[12].FindControl("ddl") as DropDownList;//取得控件 
                if (ds.Tables.Count > 0)
                {
                    string value = ds.Tables[0].Rows[e.Row.RowIndex]["states"].ToString();
                    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));//下拉框值得绑定
                    ddl.Enabled = false;
                }
            }
            
        }


        protected void gvPlan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString();
            if (e.CommandName == "Alter")//修改
            {
                int id = Convert.ToInt32(gvPlan.DataKeys[int.Parse(index)].Value);
                Response.Redirect("workPlanEdit.aspx?ID=" + id);//页面跳转
            }
            if (e.CommandName == "go")
            {

                TextBox tb = (TextBox)gvPlan.BottomPagerRow.FindControl("inPageNum");
                int num = Int32.Parse(tb.Text);
                GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                gvPlan_PageIndexChanging(null, ea);


            }
        }
        //作废按钮
        protected void bt_over_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(((Button)sender).CommandArgument.ToString());
            CustomsSystem.Model.T_workPlan tp = bll.GetModel(id);
            tp.states = 0;
            if (bll.Update(tp))
            {
                MessageBox.ShowAndRedirect(this, "作废成功", "workplanNView.aspx");
            }
            else
            {
                MessageBox.Show(this, "作废失败");
            }
        }
        //完成按钮
        protected void bt_end_Click(object sender, EventArgs e)
        {
            int id = Convert .ToInt32 (((Button)sender).CommandArgument.ToString());
            CustomsSystem.Model.T_workPlan tp = bll.GetModel(id);
            tp.states = 2;
            if (bll.Update(tp))
            {
                MessageBox.ShowAndRedirect(this, "计划完成成功", "workplanNView.aspx");
            }
            else
            {
                MessageBox.Show(this, "计划完成失败");
            }
        }

      
        protected void gvPlan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
                gvPlan.PageIndex = e.NewPageIndex;//绑定最新页
                ds = bll.GetList(Query());
                gvPlan.DataSource = ds;
                gvPlan.DataBind();
                TextBox tb = (TextBox)gvPlan.BottomPagerRow.FindControl("inPageNum");
                tb.Text = (gvPlan.PageIndex + 1).ToString();
                

         
        }

   


    }
}